// SideBySideDlg.cpp : implementation file
//

#include "stdafx.h"
#include "RigCalib.h"
#include "SideBySideDlg.h"
#include "cvvImage.h"
#include "gui_size_macro.h"
#include "cvxDeckLinkInputCallback.h"


// CSideBySideDlg dialog

IMPLEMENT_DYNAMIC(CSideBySideDlg, CDialog)

CSideBySideDlg::CSideBySideDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CSideBySideDlg::IDD, pParent)
{
	
}

CSideBySideDlg::~CSideBySideDlg()
{
}

void CSideBySideDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	DDX_Control(pDX, IDC_PIC_LEFT, m_leftPicture);
	DDX_Control(pDX, IDC_PIC_RIGHT, m_rightPicture);
	DDX_Control(pDX, IDC_SBS_SHOW_MARKER, m_sbsShowMarkerCenter);
}


BEGIN_MESSAGE_MAP(CSideBySideDlg, CDialog)
	ON_WM_PAINT()
	ON_BN_CLICKED(IDC_SDS_CAPTURE_FRAME, &CSideBySideDlg::OnBnClickedSdsCaptureFrame)
END_MESSAGE_MAP()


// CSideBySideDlg message handlers

BOOL CSideBySideDlg::OnInitDialog()
{
	CDialog::OnInitDialog();

	// TODO:  Add extra initialization here
	int w = LEFT_PICTURE_WIDTH;
	int h = LEFT_PICTURE_HEIGHT;
	m_leftPicture.MoveWindow(10, 100, w, h);
	m_rightPicture.MoveWindow(w+20, 100, w, h);

	m_leftImage  = NULL;   
	m_rightImage = NULL;

	m_sbsShowMarkerCenter.SetCheck(BST_UNCHECKED);

	return TRUE;  // return TRUE unless you set the focus to a control
	// EXCEPTION: OCX Property Pages should return FALSE
}

void CSideBySideDlg::updateImages(char *pdata, char *pRightData, int w, int h, int nChannels, 
								  const vector<CvxMarker> &leftMarkers, const vector<CvxMarker> &rightMarkers, 
								  bool flipRight)
{
	//@todo add update flag
	assert(nChannels == 2);
	assert(pdata);

	//update image formate
	if (!m_leftImage)
	{
		m_leftImage = cvCreateImage(cvSize(w, h), IPL_DEPTH_8U, 3);
	}
	else if (m_leftImage->width != w || m_leftImage->height != h)
	{
		cvReleaseImage(&m_leftImage);
		m_leftImage = cvCreateImage(cvSize(w, h), IPL_DEPTH_8U, 3);
	}

	if (!m_rightImage)
	{
		m_rightImage = cvCreateImage(cvSize(w, h), IPL_DEPTH_8U, 3);
	}
	else if (m_rightImage->width != w || m_rightImage->height != h)
	{
		cvReleaseImage(&m_rightImage);
		m_rightImage = cvCreateImage(cvSize(w, h), IPL_DEPTH_8U, 3);
	}
	
	cvxYUV422toRGB(w, h, (unsigned char *)pdata, m_leftImage, false);
	if (pRightData)
	{
		cvxYUV422toRGB(w, h, (unsigned char *)pRightData, m_rightImage, flipRight);
	}
	if (m_sbsShowMarkerCenter.GetCheck() == BST_CHECKED)
	{
		for (int i = 0; i<leftMarkers.size(); ++i)
		{
			Point2f p = leftMarkers[i].m_centerPt;
			cv::circle(Mat(m_leftImage), cvPoint(p.x, p.y), 8.0, cvScalar(0, 0, 255));
		}
		for (int i = 0; i<rightMarkers.size(); ++i)
		{
			Point2f p = rightMarkers[i].m_centerPt;
			cv::circle(Mat(m_rightImage), cvPoint(p.x, p.y), 8.0, cvScalar(0, 0, 255));
		}
	}
	
 	

	InvalidateRect(0, FALSE);
}



void CSideBySideDlg::OnPaint()
{
	CPaintDC dc(this); // device context for painting
	// TODO: Add your message handler code here
	// Do not call CDialog::OnPaint() for painting messages
	//draw left and right frame

	if (m_leftImage)
	{
		CmfcImage leftImageDrawer;		
		CDC *pDc = m_leftPicture.GetDC();	
		CRect rect;
		ASSERT(pDc);
		HDC hDc = pDc->GetSafeHdc();
		m_leftPicture.GetClientRect(&rect);
		leftImageDrawer.passImage(m_leftImage);
		leftImageDrawer.DrawToHDC(hDc, &rect);
		ReleaseDC(pDc);
	}
	if (m_rightImage)
	{
		CmfcImage rightImageDrawer;
		rightImageDrawer.passImage(m_rightImage);
		CDC *pDc = m_rightPicture.GetDC();	
		CRect rect;
		ASSERT(pDc);

		HDC hDc = pDc->GetSafeHdc();
		m_rightPicture.GetClientRect(&rect);
		rightImageDrawer.DrawToHDC(hDc, &rect);
		ReleaseDC(pDc);
	}

}

void CSideBySideDlg::OnBnClickedSdsCaptureFrame()
{
	// TODO: Add your control notification handler code here
	static int idx = 0;	
	if (m_leftImage && m_rightImage)
	{
		string leftName = string(".png");
		string rightName = string(".png");
		char buf[256] = {NULL};
		sprintf(buf, "%d", idx);
		leftName  = string("left_") + string(buf) + leftName;
		rightName = string("right_") + string(buf) + rightName;
		cvSaveImage(leftName.c_str(), m_leftImage);
		cvSaveImage(rightName.c_str(), m_rightImage);
		idx++;
	}
}
